package ext.tianma.project.processors;

import java.util.Iterator;
import java.util.List;

import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.fc.collections.WTCollection;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;

import com.ptc.core.components.forms.FormResult;
import com.ptc.netmarkets.util.beans.NmCommandBean;
import com.ptc.projectmanagement.assignment.AssignmentHelper;
import com.ptc.projectmanagement.cost.CostHelper;
import com.ptc.projectmanagement.deliverable.PlanDeliverable;
import com.ptc.projectmanagement.deliverable.PlanDeliverableLink;
import com.ptc.projectmanagement.deliverable.processors.EditDeliverableFormProcessor;
import com.ptc.projectmanagement.plan.Duration;
import com.ptc.projectmanagement.plan.Plan;
import com.ptc.projectmanagement.plan.PlanActivity;
import com.ptc.projectmanagement.plan.Plannable;
import com.ptc.projectmanagement.plannable.PlannableHelper;

public class CusEditDeliverableFormProcessor extends EditDeliverableFormProcessor{

	@Override
	public FormResult doOperation( NmCommandBean numCommandBean , List list ) throws WTException {

		return super.doOperation( numCommandBean , list );
	}

	@Override
	public FormResult postProcess( NmCommandBean numCommandBean , List list )throws WTException {
		FormResult formResult = super.postProcess( numCommandBean , list );
		
		Object object = numCommandBean.getElementContext().getTargetOid().getRefObject();
		
		if( object != null ){
			if( object instanceof ObjectReference ){
				ObjectReference orf = ( ObjectReference )object;
				object = orf.getObject();
			}
			
			if( object instanceof PlanDeliverable ){
				PlanDeliverable pd = ( PlanDeliverable )object;
				
				if( pd != null ){
					Double complete = pd.getPercentComplete();
					
					if( complete == 100 ){
						PlanActivity planActivity = getPlanActivity( pd );
						
						if( planActivity != null ){
							System.out.println( "Debug   planActivity : " + planActivity.getName() );
							//通过子活动获取所有的子活动中的PlanDeliverableLink
							WTCollection sonWTCollection = PlannableHelper.service.getPlanDeliverableLinks( planActivity );
							System.out.println( "Debug   sonWTCollection : " + sonWTCollection.size() );
							Iterator iterator = sonWTCollection.iterator();
							
							boolean isComplete = false;
							
							while( iterator.hasNext() ){
								Object obj = iterator.next();
								
								if( obj instanceof ObjectReference ){
									ObjectReference orf = ( ObjectReference )obj;
									obj = orf.getObject();
								}
								
								if( obj instanceof PlanDeliverable ){
									PlanDeliverable pldv = ( PlanDeliverable )obj;
									
									if( pldv != null ){
										Double db = pldv.getPercentComplete();
										System.out.println( "Debug   pldv : " + pldv.getDisplayIdentity() );
										if( db != 100 ){
											isComplete = true;
										}
									}
								}
							}
							System.out.println( "Debug   isComplete : " + isComplete );
							if( !isComplete ){
								
								//实际工时
								Duration duration = planActivity.getDuration();
								System.out.println( "Debug   duration ... : " + duration );
								if( duration != null )
									try {
										planActivity.setDoneEffort( duration );
										
										//通过实际工时来计算完成百分比
										planActivity = (PlanActivity) AssignmentHelper.service.calculatePercentWorkComplete( planActivity );

										planActivity = (PlanActivity)PersistenceHelper.manager.save( planActivity );
										
										if( planActivity.getDoneEffort() != null)
											AssignmentHelper.service.distributeActualWork(planActivity);
										
										planActivity = (PlanActivity)AssignmentHelper.service.rollUpEffort(planActivity);
										PlannableHelper.service.propagateSchedule( ( Plannable )planActivity.getRoot() );
										CostHelper.service.rollUpCost( planActivity );
									} catch (WTPropertyVetoException e) {
										e.printStackTrace();
									}
								
							}
							
							
						}else{
							System.out.println( "Debug  获取的planActivity为null" );
						}
					}
				}
				
			}
		}
		

		return formResult;
	}
	
	 /**
	 * 获取可交付结果相关联的的活动
	 * @param planDB
	 * @return
	 */
	public static PlanActivity getPlanActivity( PlanDeliverable planDB ){
		PlanActivity planActivity = null;
		Plan plan = null;
		boolean flag = false;
		
		if( planDB == null ){
			return planActivity;
		}
		
		try{
			//获取可交付结果的上一级
	        WTCollection wtcollection = PlannableHelper.service.getPlanDeliverableLinks( planDB );
	            
	        if( wtcollection != null ){
	            Iterator iterator = wtcollection.persistableIterator();
	            	
	            while( iterator.hasNext() ){
	            	PlanDeliverableLink plandeliverablelink = ( PlanDeliverableLink )iterator.next();
	            		
	            	planActivity = plandeliverablelink.getPlanActivity();
	            	
	            	if( planActivity != null ){
	            		break;
	            	}
	            }
	         }		
		} catch (WTException e) {
			e.printStackTrace();
		}

		return planActivity;
	}
	

}
